//引入VueRouter
import VueRouter from 'vue-router'
//引入需要路由管理的组件
import NovelMain from '@/components/NovelMain'


const router = new VueRouter({
    routes: [
        {
            name: 'main',
            path: '/',
            redirect: {name: 'home'},
            component: NovelMain,
            children: [
                {
                    name: 'home',
                    path: 'home',
                    component: () => import('@/page/NovelHome'),
                },
                {
                    name: 'oeuvre',
                    path: 'oeuvre',
                    component: () => import('@/page/NovelOeuvre'),
                },
                {
                    name: 'order',
                    path: 'order',
                    component: () => import('@/page/NovelOrder'),
                    redirect: {
                        path: '/order/allSort', query: {
                            catalog: "全部分类",
                        },
                    },
                    children: [
                        {
                            name: 'allSort',
                            path: 'allSort',
                            component: () => import('@/page/NovelAllSort'),
                        },
                        {
                            name: 'sortPage',
                            path: 'sortPage',
                            component: () => import('@/page/NovelSortPage'),
                        },
                    ]
                },
            ]
        },
        {
            name: 'bookshelf',
            path: '/bookshelf',
            component: () => import('@/page/NovelBookshelf'),
            meta: {
                inspect: true
            }
        },
        {
            name: 'login',
            path: '/login',
            component: () => import('@/page/NovelLogin'),
        },
        {
            name: 'register',
            path: '/register',
            component: () => import('@/page/NovelRegister'),
        },
        {
            name: 'book',
            path: '/book/:id',
            component: () => import('@/page/NovelBookPage'),
        },
        // {
        //     name: 'chapter',
        //     path: '/chapter/:bookId/:chapterId',
        //     component: () => import('@/page/NovelChapter'),
        // },
        {
            name: 'chapter',
            path: '/chapter/:bookId/:chapterSort',
            component: () => import('@/page/NovelChapter'),
        },
        {
            name: 'userPage',
            path: '/userPage',
            component: () => import('@/page/NovelUserPage'),
            redirect: {path: '/userPage/userInfoPage'},
            children: [
                {
                    name: 'userInfoPage',
                    path: 'userInfoPage',
                    component: () => import('@/page/NovelUserInfoPage'),
                    meta: {
                        inspect: true
                    },
                },
                {
                    name: 'userSetting',
                    path: 'userSetting',
                    component: () => import('@/page/NovelUserSetting.vue'),
                    meta: {
                        inspect: true
                    },
                },
                {
                    name: 'accountingCenter',
                    path: 'accountingCenter',
                    component: () => import('@/page/NovelAccountingCenter.vue'),
                    meta: {
                        inspect: true
                    },
                },
            ],
        },
        {
            name: 'writer',
            path: '/writer',
            component: () => import('@/page/NovelWriter'),
            redirect: {path: '/writer/workbench'},
            children: [
                {
                    name: 'workbench',
                    path: 'workbench',
                    component: () => import('@/page/NovelWorkbench'),
                },
                {
                    name: 'workManagement',
                    path: 'workManagement',
                    component: () => import('@/page/NovelWorkManagement'),
                },
                {
                    name: 'createBook',
                    path: 'createBook/:bookId',
                    component: () => import('@/page/NovelCreateBook'),
                },
            ]
        },
        {
            name: 'editBook',
            path: '/editBook/:bookId',
            component: () => import('@/page/NovelEditBook'),
        },


        {
            name: 'adminIndex',
            path: '/admin',
            // redirect: { name: 'home' },
            component: () => import('@/page/admin/NovelAdminIndexPage'),
            children: [
                {
                    name: 'adminRecom',
                    path: '/adminRecom',
                    component: () => import('@/page/admin/NovelAdminRecom'),
                },
                {
                    name: 'adminUser',
                    path: '/adminUser',
                    component: () => import('@/page/admin/NovelAdminUser'),
                },
                {
                    name: 'adminUserComments',
                    path: '/adminUserComments',
                    component: () => import('@/page/admin/NovelAdminUserComments'),
                },
                {
                    name: 'adminBook',
                    path: '/adminBook',
                    component: () => import('@/page/admin/NovelAdminBook'),
                },

                /*{
                    name: 'home',
                    path: 'home',
                    component: () => import('@/page/NovelHome'),
                },
                {
                    name: 'oeuvre',
                    path: 'oeuvre',
                    component: () => import('@/page/NovelOeuvre'),
                },
                {
                    name: 'order',
                    path: 'order',
                    component: () => import('@/page/NovelOrder'),
                    redirect: { path: '/order/allSort' },
                    children: [
                        {
                            name: 'allSort',
                            path: 'allSort',
                            component: () => import('@/page/NovelAllSort'),
                        },
                        {
                            name: 'sortPage',
                            path: 'sortPage',
                            component: () => import('@/page/NovelSortPage'),
                        },
                    ]
                },*/
            ]
        },

    ],
})

//全局路由守卫
router.beforeEach((to, from, next) => {
    console.log('beforeEach', to, from)
    if (to.meta.inspect) {
        console.log("进入当前路由需要登录")
        let userInfo = window.sessionStorage.getItem('userInfo')
        console.log("是否登录", userInfo)
        if (userInfo) {
            //用户信息存在         
            next()
        } else {
            //用户信息获取失败跳转至登录页面
            //TODO:开发状态暂时注释
            next({
                path: '/login'
            })
        }

    }
    next()
})

export default router


